package io.renren.modules.wyzsphone.service.Impl;

import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.wyzsphone.commWithWCF.CommonEncryptUtil;
import io.renren.modules.wyzsphone.dao.FactoryUserDao;
import io.renren.modules.wyzsphone.entity.FactoryUser;
import io.renren.modules.wyzsphone.service.FactoryUserService;
import io.renren.modules.wyzsphone.utils.Constant;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FactoryUserServiceImpl implements FactoryUserService {

    @Autowired
    FactoryUserDao facortyUserDao;
    @Override
    public void saveFactoryUser(Map<String, Object> params) throws Exception {

        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        //加密
        String password= (String) params.get("password");
        password = CommonEncryptUtil.aesEncrypt(password, Constant.SALT);
        params.remove("password");
        params.put("username",username);
        params.put("password",password);

        facortyUserDao.saveFactoryUser(params);

    }

    public void saveFactoryLoginInfo(Map<String, Object> params) throws Exception{

        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        params.put("username",username);

        facortyUserDao.saveFactoryLoginInfo(params);

    }


    @Override
    public void changeFactoryUserPass(Map<String,Object>params) throws Exception{

        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        //加密
        String password= (String) params.get("password");
        password = CommonEncryptUtil.aesEncrypt(password, Constant.SALT);
        params.remove("password");
        params.put("username",username);
        params.put("password",password);

        facortyUserDao.changeFactoryUserPass(params);
    }

    @Override
    public List<FactoryUser> getMyFactoryList() {
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        return facortyUserDao.getMyFactoryList(username);
    }

    @Override
    public FactoryUser getMyFactoryById(String factoryId) {
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        return facortyUserDao.getMyFactoryById( username,factoryId);
    }

    @Override
    public void deleteFactoryUser(String factoryId) {
        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        facortyUserDao.deleteFactoryUser(username,factoryId);
    }

    @Override
    public FactoryUser getUserByFactoryId(Map<String, Object> params) {

        //String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
       //params.put("username",username);

        return facortyUserDao.getUserByFactoryId(params);
    }
}
